package jp.co.kayo.android.twittertimeline;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ListActivity;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ListActivity {
    private TimelineAdapter adapter;
    private ImageView twittericon;
    private TextView txtname;
    private TextView txttweet;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        twittericon = (ImageView)findViewById(R.id.TwitterIcon);
        txtname = (TextView)findViewById(R.id.txtname);
        txttweet = (TextView)findViewById(R.id.txttweet);
        
        ArrayList<Tweet> tweets = new ArrayList<Tweet>();
        adapter = new TimelineAdapter(this, tweets);
        setListAdapter(adapter);
        
        Utils.resetTimeline(this);
        
        new GetProfile().execute();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadData();
    }
    
    private void loadData(){
        new GetTimelineTask().execute();
    }
    
    private class GetProfile extends AsyncTask<Void, Profile, Void>{

        @Override
        protected Void doInBackground(Void... mParams) {
            Profile profile = Utils.getProfile();
            if(profile!=null){
                publishProgress(profile);
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Profile ...progress){
            if(progress.length>0){
                Profile profile = progress[0];
                twittericon.setImageDrawable(Utils.getImageDrawable(profile.getImageurl()));
                txtname.setText(profile.getName());
                txttweet.setText(profile.getStatuses_count());
            }
        }
        
    }
    
    private class GetTimelineTask extends AsyncTask<Void, Tweet, Void>{
        
        @Override
        protected void onPreExecute(){
            super.onPreExecute();
            adapter.clear();
        }

        @Override
        protected Void doInBackground(Void... mParams) {
            String json = Utils.getTimeLine(MainActivity.this);
            try {
                JSONArray array = new JSONArray(json);
                for(int i=0; i<array.length(); i++){
                    JSONObject obj = array.getJSONObject(i);
                    
                    Tweet tweet = new Tweet();
                    tweet.setCreated_at(obj.getString("created_at"));
                    tweet.setText(obj.getString("text"));
                    
                    publishProgress(tweet);
                }
                
            } catch (JSONException e) {
                Logger.e("GetTimelineTask", e);
            }
            
            
            return null;
        }
        
        @Override
        protected void onProgressUpdate(Tweet ...progress){
            for(Tweet tweet : progress){
                adapter.add(tweet);
            }
            adapter.notifyDataSetChanged();
        }
        
    }
}